/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rooster.encoder;

import org.rooster.dbmanager.repository.RoosterRepository;
import org.rooster.schedule.Resource;
import org.rooster.schedule.ResourceType;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: manorama
 * Date: 8/7/12
 * Time: 3:37 PM
 * To change this template use File | Settings | File Templates.
 */
public class Encoder {

    private static Encoder instance;
    private HashMap<Integer, HashMap> mainResourceTypeMap;
    private int volumeCount, nonVolumeCount;
    private int volumeUpperLimit, volumeLowerLimit, nonVolumeUpperLimit, nonVolumeLowerLimit;

    private Encoder() {
        init();
    }

    public static Encoder getInstance() {
        if (instance == null) {
            instance = new Encoder();
        }
        return instance;
    }

    /**
     * Initialize the encoder by retrieving the available resources from the database
     */
    private void init() {
        volumeCount = 1;
        nonVolumeCount = 1;
        mainResourceTypeMap = new HashMap<Integer, HashMap>();


        Set resourceTypeSet = RoosterRepository.getInstance().getResourceTypes();
        Iterator resourceTypeIterator = resourceTypeSet.iterator();

        while (resourceTypeIterator.hasNext()) {
            ResourceType resourceType = (ResourceType) resourceTypeIterator.next();
            mainResourceTypeMap.put(resourceType.getResourceTypeID(), new HashMap<Integer, HashMap>());

            Set resourcesSet = RoosterRepository.getInstance().getResourcesByResourceType(resourceType.getResourceTypeID());
            Iterator iterator = resourcesSet.iterator();
            while (iterator.hasNext()) {

                Resource resource = (Resource) iterator.next();
                mainResourceTypeMap.get(resource.getResourceTypeId()).put(resource.getResourceTypeId(), resource);

            }

        }

        // can't we get these dynamically???and what  is the relationship between these?


        volumeLowerLimit = 1;
        volumeUpperLimit = volumeCount - 1;
        nonVolumeLowerLimit = volumeCount;   // nonVolume value depends on the volume value??
        nonVolumeUpperLimit = volumeCount + nonVolumeCount - 2;
    }

    /**
     * Returns the lowest encoded id corresponding to resource type
     * @param resourceType
     * @return
     */
    public int lowerLimitForResourceType(int resourceType) {
        if (resourceType == 1) {
            return volumeLowerLimit;
        } else {
            return nonVolumeLowerLimit;
        }
    }

    /**
     * Returns the highest encoded id corresponding to resource type
     * @param resourceType
     * @return
     */
    public int upperLimitForResourceType(int resourceType) {
        if (resourceType == 1) {
            return volumeUpperLimit;
        } else {
            return nonVolumeUpperLimit;
        }
    }

    /**
     * returns the real ID of the encoded resource id
     * @param encodedId
     * @return
     */
    public int getResourceId(int encodedId, int resourceType) {
        if (resourceType == 1) {
            //     return volumeMap.get(encodedId).getResourceID();
        } else {
            //      return nonVolumeMap.get(encodedId - volumeUpperLimit).getResourceID();
        }

        return 1;
    }
}
